﻿
using System;
using Qy_CSharp_NetWork.Component;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NetWorkController : MonoBehaviour
{
    public NetWorkDataHandle Receive { get { return m_netWorkDataHandle; } }
    private NetWorkDataHandle m_netWorkDataHandle;
    public JisightUnityComponent_LAM Send { get { return m_jisightForUnity; } }
    private JisightUnityComponent_LAM m_jisightForUnity;

    public static NetWorkController instance { get; private set; }

    private void Awake()
    {
        instance = this;
        m_jisightForUnity = this.gameObject.AddComponent<JisightUnityComponent_LAM>();
        m_netWorkDataHandle = this.gameObject.AddComponent<NetWorkDataHandle>();
        m_jisightForUnity.JLAM_Event_Fir += m_netWorkDataHandle.JisightNetWorkHandle;
        m_jisightForUnity.needDebug = true;
    }

    //private void Update()
    //{
    //    if (Input.GetKeyDown(KeyCode.Space))
    //    {
    //        Debug.Log("Send !!!!!");
    //        string[] receiver = new string[] { "All" };
    //        m_jisightForUnity.PushMsgToOther("这是一条数据！！！", receiver);
    //    }
    //}
    public void SendReadyTime(int pReadyTime)
    {
        object readyInfo = new
        {
            type = (int)PUSH_MESSAGE_TYPE.BROADCAST_READY_TIME,
            readyTime = pReadyTime
        };
        string[] receiver = new string[] { "All" };
        m_jisightForUnity.PushMsgToOther(readyInfo, receiver);
    }
    public void SenGameTime(int pGameTime)
    {
        object readyInfo = new
        {
            type = (int)PUSH_MESSAGE_TYPE.BROADCAST_GAME_TIME,
            gameTime = pGameTime
        };
        string[] receiver = new string[] { "All" };
        m_jisightForUnity.PushMsgToOther(readyInfo, receiver);
    }

    //排行榜的积分计算信息推送
    private void m_PushRankListToMaster(List<PlayerBody> lastPlayerList)
    {
        Debug.Log("向服务器推送 排行榜 信息。");
        List<string> userIdList = new List<string>();
        for (int idex = 0; idex < lastPlayerList.Count; ++idex)
        {
            //userIdList.Add(lastPlayerList[idex].userId);
        }
        if (lastPlayerList.Count > 0)
        {
            m_jisightForUnity.PushRankListToMaster(userIdList);
        }
        else
        {
            Debug.Log("没有人加入游戏。");
        }
    }

    private void m_PostAwardList(List<PlayerBody> lastPlayerList)
    {
        Debug.Log("开始 请求奖励。");
        Dictionary<string, int> userIdAwardList = new Dictionary<string, int>();
        for (int idex = 0; idex < lastPlayerList.Count; ++idex)
        {
            //userIdAwardList.Add(lastPlayerList[idex].userId, 5);
        }
        if (lastPlayerList.Count > 0)
        {
            m_jisightForUnity.PostAwardeList(userIdAwardList);
        }
        else
        {
            Debug.Log("没有人加入游戏。 取消奖励请求");
        }
    }

    // 神秘大奖
    private void m_PostMysteriousAwade(List<PlayerBody> lastPlayerList)
    {
        Debug.LogWarning("发送神秘大奖：" + lastPlayerList.Count);
        Dictionary<string, int> mysteriousDic = new Dictionary<string, int>();
        for (int idex = 0; idex < lastPlayerList.Count; ++idex)
        {
            // mysteriousDic.Add(lastPlayerList[idex].userId, 1);
        }
        // m_jisightForUnity.PushMysteriousAwardeInfo(mysteriousDic, GameController.mysteriousTime.ToString());
    }
}
